// pages/event/reporting/reportingdetails/reportingdetails.js
import Toast from "../../../../miniprogram_npm/@vant/weapp/toast/toast";
import Dialog from "../../../../miniprogram_npm/@vant/weapp/dialog/dialog";
var util = require('../../../../utils/util.js');
const app = getApp()
const log = new app.appLogs()
const http = new app.appHttp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        withdraw: false,
        event_id: null,
        event_type: null,
        unit_code: null,
        yuYinPath: '/common/image/yuyin.png',
        event: null,
        lyFile: null,
        time: null,
        imageurl: null,
        fileList: [],
        fileList1: [],
        radio: '1',
        textareaValue: null,
        textarea: null,
        text: null,
        TypeList: [],
        Type: null,
        TypeStr: null,
        TypeShow: false,
        area_code: null,
        evaluate: 5,
        status: null,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            event_id: options.id
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getReporting()
        this.data.audio = wx.createInnerAudioContext()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        const than = this
        than.data.audio.pause()
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    /**
     * 请求详情
     */
    getReporting() {
        const than = this
        const newTime = util.formatDateTime(new Date())
        http.get("/event/info/detail", {
            event_id: than.data.event_id
        }).then((res) => {
            than.setData({
                event: res.data.data,
                lyFile: res.data.data.report_url_list_voice[0],
                imageurl: res.data.data.report_url_list_pic,
                area_code: res.data.data.area_code
            })
            //300000 5分钟
            const time = Date.parse(newTime.replace(/-/g, '/'));
            const create_time = Date.parse(res.data.data.create_time.replace(/-/g, '/'));
            if (res.data.data.handle_state == 1) {
                if (create_time + 300000 >= time) {
                    than.setData({
                        withdraw: true
                    })
                } else {
                    than.setData({
                        withdraw: false
                    })
                }
            } else {
                than.setData({
                    withdraw: false
                })
            }
        })
    },
    yuYinAnimation: function () {
        let i = 1
        this.yuyinTimer = setInterval(() => {
            i++
            i = i % 3
            this.setData({
                isYuYinIndex: i + 1,
            })
            if (i === 0) {
                this.setData({
                    yuYinPath: '/common/image/yuyin.png'
                })
            } else {
                this.setData({
                    yuYinPath: '/common/image/yuyin_start_' + i + '.png'
                })
            }
        }, 400)
    },
    onYuYinPlay: function (e) {
        const than = this
        if (than.data.isYuYinPlay) {
            //正在播放，准备停止的状态
            clearInterval(than.yuyinTimer)
            than.data.audio.stop()
            than.setData({
                yuYinPath: '/common/image/yuyin.png',
                isYuYinPlay: false
            })
        } else {
            //没播放，准备播放的状态
            than.setData({
                audio: wx.createInnerAudioContext(),
                isYuYinPlay: true
            })
            than.data.audio.src = than.data.lyFile
            than.data.audio.play()
            than.yuYinAnimation()
            than.data.audio.onEnded(() => {
                clearInterval(than.yuyinTimer)
                than.setData({
                    yuYinPath: '/common/image/yuyin.png',
                    isYuYinPlay: false
                })
            })
        }
    },
    setSupplement: function (e) {
        const than = this
        const event_id = than.data.event_id
        wx.navigateTo({
            url: '../reportingdetails/supplement/supplement?event_id=' + event_id,
        })
    },
    /**
     * 撤回
     * @param e
     */
    onWithdraw: function (e) {
        const than = this
        const event_id = than.data.event_id
        Dialog.confirm({
            title: "提示",
            message: "确定要测回这条上报记录吗？"
        }).then(() => {
            http.putJson("event/info/handle", {
                event_id: than.data.event_id,
                action: "3"
            }).then((res) => {
                if (res.data.code == 200) {
                    Toast.success({
                        message: res.data.msg
                    })
                    setTimeout(res=>{
                        wx.navigateBack({
                          delta: 2,
                        })
                    },1000)
                } else {
                    Toast.fail({
                        message: "撤销失败！错误代码：" + res.data.msg + ",请联系管理员!"
                    })
                }
            })
        })
    },
    /**
     * 评价
     * @param {*} e 
     */
    setEvaluate:function(e){
        const than = this
        const event_id = than.data.event_id
        wx.navigateTo({
            url: '../reportingdetails/evaluate/evaluate?event_id=' + event_id,
        })
    }
})